from flask import current_app, jsonify
from flask import render_template
from flask import request
from flask import session

from info import constants
from info.models import User, News, Category
from info.utils.response_code import RET
from . import index_blu
# from info import redis_store

@index_blu.route('/news_list')
def news_list():
    """
    获取首页数据
    :return:
    """
    # 1.获取参数
    # 新闻的分类ｉｄ
    cid = request.args.get("cid","1")
    page = request.args.get("page","1")
    per_page = request.args.get("per_page","10")
    # 校验参数
    try:
        page = int(page)
        per_page = int(per_page)
        cid = int(cid)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.PARAMERR,errmsg="参数错误")
    filters = [News.status == 0]
    if cid != 1: # 查询的不是最新数据
        # 需要添加条件
        filters.append(News.category_id == cid)
    # 3.查询数据
    try:
        paginate = News.query.filter(*filters).order_by(News.create_time.desc()).paginate(page,per_page,False)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DATAERR,errmsg="数据查询错误")
    # 取到当前页的数据
    news_model_list = paginate.items # 模型对象列表
    total_page = paginate.pages
    current_page = paginate.page

    news_dict_li = []
    for news in news_model_list:
        news_dict_li.append(news.to_basic_dict())

    data = {
        "news_dict_li": news_dict_li,
        "total_page": total_page,
        "current_page": current_page
    }

    return jsonify(errno=RET.OK,errmsg="OK",data=data)




@index_blu.route('/')
def index():
    # 显示首页
    # # 向redis中保存一个值 name itcast
    # redis_store.set("name","itcast")
    # 获取当前登陆用户的id
    user_id = session.get("user_id",None)
    # 通过id获取用户信息
    user = None
    if user_id:
        try:
            user = User.query.get(user_id)
        except Exception as e:
            current_app.logger.error(e)

    # 右侧的新闻排行逻辑
    news_list = []
    try:
        news_list = News.query.order_by(News.clicks.desc()).limit(constants.CLICK_RANK_MAX_NEWS)
    except Exception as e:
        current_app.logger.error(e)
    news_dict_li = []
    # 遍历对象列表，将对象的字典添加到字典列表中
    for news in news_list:
        news_dict_li.append(news.to_basic_dict())
    # 查询分类数据，通过模板的形式渲染出来
    categories = Category.query.all()
    category_li = []
    for category in categories:
        category_li.append(category.to_dict())



    data = {
        "user": user.to_dict() if user else None,
        "news_dict_li":news_dict_li,
        "category_li":category_li
    }
    return render_template("news/index.html", data=data)

@index_blu.route('/favicon.ico')
def favicon():
    return current_app.send_static_file('news/favicon.ico')